package com.zw.pdm.module.est.service;

import com.zw.pdm.entity.craft.CraMakeDetail;
import com.zw.pdm.entity.designtask.DsnProStruct;
import com.zw.pdm.entity.est.Card;
import com.zw.pdm.entity.est.CardChange;
import com.zw.pdm.entity.est.CardDetail;
import com.zw.pdm.entity.est.CardDetailChange;
import com.zw.pdm.module.craft.repository.CraftMakeDetailRepository;
import com.zw.pdm.module.designtask.repository.DsnProStructRepository;
import com.zw.pdm.module.est.mapper.CardDetailMapper;
import com.zw.pdm.module.est.mapper.CardMapper;
import com.zw.pdm.module.est.model.CraMakeSheetModel;
import com.zw.pdm.module.est.repository.CardChangeRepository;
import com.zw.pdm.module.est.repository.CardDetailChangeRepository;
import com.zw.pdm.module.est.repository.CardDetailRepository;
import com.zw.pdm.module.est.repository.CardRepository;
import com.zw.pdm.module.system.service.LogService;
import com.zw.pdm.util.CommonUtils;
import com.zw.pdm.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import static com.zw.pdm.util.BeanUtil.copy;


/**
 * @Description:
 *
 * @Author: China.sgl
 * @Date: 2019/12/6 08:42
 */
@Service
@SuppressWarnings("all")
public class ChaWarnService {

    @Autowired
    private CardRepository cardRepository;
    @Autowired
    private CardMapper cardMapper;
    @Autowired
    private CardDetailMapper cardDetailMapper;
    @Autowired
    private CardDetailRepository cardDetailRepository;
    @Autowired
    private CardChangeRepository cardChangeRepository;
    @Autowired
    private CardDetailChangeRepository cardDetailChangeRepository;
    @Autowired
    private CraftMakeDetailRepository craftMakeDetailRepository;
    @Autowired
    private LogService logService;
    @Autowired
    private DsnProStructRepository dsnProStructRepository;

    /** 列表 */
    public List<CraMakeSheetModel> findWithChaWarn(CraMakeSheetModel craMakeSheetModel) {
        return cardMapper.findWithChaWarn(craMakeSheetModel);
    }

    /** 处理变更提醒 */
    /**
     * 按照科长与工艺科核对业务，变更提醒功能注销不要了。但变更提醒功能下存档部分业务处理
     * 首先将制作件清单的图号在变更删除后，通过变更提醒页面进行删除。取消后业务只能搬迁到制作件清单接收页面
     * 但其他字段目前无法判断是否还有其他问题，只能先改一部分。
     * @param id
     * @return
     */
    public int handler(Integer id) {
        logService.saveLog("变更提醒完结", "清单从表ID" + id);
        CraMakeDetail detail = craftMakeDetailRepository.getOne(id);
        if (detail.getTechnologyChange() == 2) {
            detail.setIsDel(1);
        }
        detail.setChangeStatus(1);
        detail.setChangeType(0);
        detail.setTechnologyChange(0);
        craftMakeDetailRepository.saveAndFlush(detail);
        return 1;
        //return cardMapper.updateMakeDetailChangeStatusById(id);
    }

    /** 查询工艺卡 */
    public Card getCardById(Integer id) {
        return cardRepository.getOne(id);
    }

    /** 查工艺卡详细信息 */
    public List<CardDetail> findCardDetailByCardId(Integer id) {
        return cardDetailMapper.findByCardId(id);
    }
    /** 查工艺卡详细信息和转序状态 */
    public List<CardDetail> findCardDetailAndTranByCardId(Integer id) {
        return cardDetailMapper.findCardDetailAndTranByCardId(id);
    }

    /** 变更工艺卡 */
    @Transactional(rollbackFor = Exception.class)
    public int update(Card card, Integer[] detailId, String[] route, Integer[] orderNo, String[] process, String[] equName, String[] craftContent) {
        DsnProStruct dsnProStruct = dsnProStructRepository.getOne(card.getPartId());
        card.setPart(dsnProStruct.getProPart());
        Card target = cardRepository.getOne(card.getId());
        CardChange cc = saveChangeCard(card, target);
        copy(card, target);
        target.setSpec(card.getSpec());
        target.setStatus(1);
        target.setMayChange(1);
        //target.setVerifyCode(null);
        //target.setVerifyName(null);
        target.setVerifyTime(null);
        target.setIsCheck(0);
        target.setQuotaCode(null);
        target.setQuotaName(null);
        target.setQuotaBranchName(null);
        target.setQuotaTime(null);
        target.setTurnQuotaTime(null);
        target.setIsQuota(0);
        target.setCommitTime(null);
        target.setNotifyTime(null);
        target.setUpdateCode(ShiroUtils.getLoginName());
        target.setUpdateName(ShiroUtils.getUser().getName());
        target.setUpdateTime(new Date());

//        target.setCracReceiveStatus(0);
        target.setCracReceiveName(null);
        target.setCracReceiveTime(null);
        target.setProdChange(1);
        cardRepository.save(target);

        // 删除工时表数据
        cardMapper.deleteWorkTimeByCardId(target.getId());
        // 删除工艺路线表数据
        cardMapper.delCraftRouteByCardId(target.getId());
        // 修改制作件清单主表 审核人， 定额状态， 转定额时间， 提交状态， 全部提交时间
        //cardMapper.updateMakeSheetByCardId(target.getId());
        //cardMapper.updateCraMakeSheetChangeByCardId(target.getId());
        // 修改制作件清单分配表 同意状态
        //cardMapper.updateReleaseDetailIsSuccessByCardId(target.getId());
        // 修改制作件清单从表 处理状态
        cardMapper.updateMakeDetailChangeStatusByCardId(target.getId());

        //创建变更单时，不能修改craMakeSheet表的状态
        //cardMapper.updateCraMakeSheetProd(target.getId());

        for (int i = 0; i < route.length; i++) {
            CardDetail detail = new CardDetail();
            CardDetailChange cdc = new CardDetailChange();
            cdc.setCardChangeId(cc.getId());
            cdc.setCreateTime(new Date());
            cdc.setIsDel(0);
            if (detailId.length > 0) {
                if (detailId[i] == null) {
                    detail.setCardId(target.getId());
                    detail.setCreateTime(new Date());
                    detail.setMayChange(0);
                    detail.setIsDel(0);
                } else {
                    detail = cardDetailRepository.getOne(detailId[i]);
                }
            } else {
                detail.setCardId(target.getId());
                detail.setCreateTime(new Date());
                detail.setMayChange(0);
                detail.setIsDel(0);
            }

            cdc.setRoute(detail.getRoute());
            cdc.setOrderNo(detail.getOrderNo());
            cdc.setProcess(detail.getProcess());
            cdc.setEquName(detail.getEquName());
            cdc.setCraftContent(detail.getCraftContent());
            cdc.setRouteCode(detail.getRouteCode());

            cdc.setNewRouteCode(null);
            cdc.setNewRoute(route[i]);
            cdc.setNewOrderNo(orderNo[i]);
            cdc.setNewProcess(process[i]);
            cdc.setNewCraftContent(CommonUtils.handlerEnterAndSpace(craftContent[i]));

            detail.setOrderNum(i);
            detail.setRoute(route[i]);
            detail.setOrderNo(orderNo[i]);
            detail.setProcess(process[i]);
            if (equName.length > 0) {
                detail.setEquName(equName[i]);
                cdc.setNewEquName(equName[i]);
            } else {
                detail.setEquName("");
                cdc.setNewEquName("");
            }
            detail.setCraftContent(CommonUtils.handlerEnterAndSpace(craftContent[i]));
            //detail.setWorkTimeOne(null);
            //detail.setWorkTimeTwo(null);
            detail = cardDetailRepository.save(detail);
            cardDetailChangeRepository.save(cdc);
        }
        logService.saveLog("变更工艺卡", "工艺卡ID：" + card.getId());
        return 1;
    }

    /** 保存变更信息 */
    private CardChange saveChangeCard(Card card, Card target) {
        CardChange cc = new CardChange();
        cc.setCardId(card.getId());
        cc.setCreateTime(new Date());
        cc.setIsDel(0);
        cc.setNewPartId(card.getPartId());
        cc.setNewPart(card.getPart());
        cc.setNewDrawNo(card.getDrawNo());
        cc.setNewDrawName(card.getDrawName());
        cc.setNewDrawNum(card.getDrawNum());
        cc.setNewQuality(card.getQuality());
        cc.setNewSpec(card.getSpec());
        cc.setNewWeight(card.getWeight());
        cc.setQuality(target.getQuality());
        cc.setSpec(target.getSpec());
        cc.setWeight(target.getWeight());
        cc.setDrawNo(target.getDrawNo());
        cc.setDrawName(target.getDrawName());
        cc.setDrawNum(target.getDrawNum());
        cc.setPartId(target.getPartId());
        cc.setPart(target.getPart());
        cc.setVerifyTime(target.getVerifyTime());
        cc.setVerifyName(target.getVerifyName());
        cc.setUpdateTime(target.getUpdateTime());
        cc.setUpdateName(target.getUpdateName());
        cc.setQuotaTime(target.getQuotaTime());
        cc.setQuotaName(target.getQuotaName());
        cc.setQuotaCode(target.getQuotaCode());
        cc.setNotifyTime(target.getNotifyTime());
        cc.setCommitTime(target.getCommitTime());
        cc.setCreateCodeOne(target.getCreateCodeOne());
        cc.setCreateNameOne(target.getCreateNameOne());
        cc.setCreateTel(target.getCreateTel());
        cc.setCreateCodeTwo(target.getCreateCodeTwo());
        cc.setCreateNameTwo(target.getCreateNameTwo());
        return cardChangeRepository.save(cc);
    }

    /** 查询 dsn_make_change_detail 表的 sheetID */
    public Integer findHrefId(Integer id) {
        return cardMapper.findHrefId(id);
    }

    /** 查询 dsn_draw_change_detail 的 sheetId */
    public Integer findHrefId2(Integer id) {
        return cardMapper.findHrefId2(id);
    }
}
